<?php
namespace app\index\controller;

use think\Db;                       // db
use think\Session;
use app\common\controller\HomeBase;  // model
use app\common\model\Jumpcategory as JumpcategoryModel;
use app\common\model\Audit as AuditModel;

class Shenhe extends HomeBase
{
    protected $wacategory_model;
    protected $wenan_model;

    protected function _initialize(){
        parent::_initialize();
        $this->wacategory_model = new JumpcategoryModel;
        $this->wenan_model = new AuditModel;
    }

    public function index($id = '' ,$keyword = '' , $sort = 0 , $type = 0 ,$page = 1)
    {
        $category = [];
        $where = ['type' => ['eq',$type],'examine' => ['eq',1],'status' => 1];
        if ($sort == 0) {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }else if($sort == 1){
            $order = 'create_time DESC';
        } else if ($sort == 2) {
            $order = 'downloadding DESC,readding DESC';
        } 
        // else if ($sort == 3) {
        //     $order = 'readding DESC';
        // }
         else {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }

        if(!empty($keyword)){
            // $where['title|abstract'] = ['like','%'.$keyword.'%'];
            // $article_list = $this->wenan_model->where($where)->order($order)->paginate(20,false,['page' => $page]);
        }else{
            if(empty($id)) $id = 10;
            
            $category = $this->wacategory_model->where('pid',0)->select();
            $article_list = $this->wenan_model->where($where)->where('wid',$id)->order($order)->paginate(20,false,['page' => $page]);
        }
        // $article_list = $this->wenan_model->query("SELECT * FROM `think_wenan` WHERE `type` = {$type} AND  `wid` IN (SELECT id FROM think_wacategory WHERE pid = {$id}) ORDER BY `id` DESC")->paginate(2,false,['page' => $page]);
        $this->assign('article_list',$article_list);
        $this->assign('type',$type);
        $this->assign('sort',$sort);
        $this->assign('id',$id);
        $this->assign('nav',3);
        $this->assign('page',$page);
        $this->assign('keyword',$keyword);
        $this->assign('category',$category);
        return $this->fetch('shenhe');
    }
}
